<?php
namespace App\Modules\Relationship\Models;


use App\Modules\Relationship\Interfaces\ModelInterface;
use App\Http\Services\CommonServices;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\Base;

class FriendsModel extends Base implements ModelInterface {

    protected $table = "friends_follow";
    protected $fields = ['id', 'user_id as userId',
        'follow_user_id as followUserId',
        'intimate', 'create_time as createTime',
        'create_timestamp as createTimestamp',
        'level','update_time as updateTime',
        'is_deleted as isDeleted',
        'state'
    ];

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    // 获取某条用户关系记录
    public function getRecord(int $userId, int $followUserId){
        $condition = array(
            "user_id"=>$userId,
            "follow_user_id"=>$followUserId,
            "is_deleted"=>0
        );
        $return = $this->getOneByCondition($condition, $this->fields);
        return $return;
    }

    // 查询一组记录
    public function getGroupRecord ($userId, $friendId) {
        $re1 = $this->getRecord($userId, $friendId);
        $re2 = $this->getRecord($friendId, $userId);
        if (!$re1 || !$re2) {
            return null;
        }
        return [$re1, $re2];
    }

    public function getone($limit) {
        $fields = implode(',', $this->fields);
        $sql = "SELECT {$fields} FROM zb_friends_follow where state = 0 Limit 1";
        $record = DB::select($sql);
        return self::objToArray($record[0]);
    }

    public function updateone($id, $data) {
        DB::table($this->table)->where(array('id'=>$id))->update($data);
    }


    public function createRecord (array $data){
        return DB::table($this->table)->insertGetId($data);
    }

    public function updateRecord (array $ids, array $data){
        return DB::table($this->table)->whereIn('id',  $ids)->update($data);
    }

    public function deleteRecord (array $ids) {
        return DB::table($this->table)->whereIn('id',  $ids)->update(array('is_deleted'=>1));
    }


    public function addIntimate (int $id, int $intimate) {
        return DB::table($this->table)->where('id', $id)->increment('intimate', $intimate);
    }

}

